This section describes some additional useful data types, functions and macros of LEDA. They can be used in any program that includes the <LEDA/basic.h> header file.
7.1 Streams
The stream data types described in this section are all derived from the stream types istream and ostream. Some of these types may be obsolete in combination with the latest versions of the standard I/O library.
7.1.1 File input streams (file_istream)
1. Definition
An instance I of the data type file is an istream connected to a file F, i.e., all input operations or operators applied to I read from F.
2. Creation
file I(string s);
creates an instance I of type file_istream connected to the file with name s.
3. Operations
All operations and operators (> >) defined for istreams can be applied to file input streams as well.
7.1.2 File output streams (file_ostream)
1. Definition
An instance O of the data type file is an ostream connected to a file F, i.e., all output operations or operators applied to O write to F.
2. Creation
file O(string s);
creates an instance O of type file_ostream connected to the file with name s.
3. Operations
All operations and operators (< <) defined for ostreams can be applied to file output streams as well.
7.1.3 String input streams (string_istream)
1. Definition
An instance I of the data type string is an istream connected to a string s, i.e., all input operations or operators applied to I read from s.
2. Creation
string I(string s);
creates an instance I of type string_istream connected to the string s.
3. Operations
All operations and operators (> >) defined for istreams can be applied to string input streams as well.
7.1.4 String output streams (string_ostream)
1. Definition
An instance O of the data type string is an ostream connected to an internal string buffer, i.e., all output operations or operators applied to O write into this internal buffer. The current value of the buffer is called the contents of O.
2. Creation
string O;
creates an instance O of type string_ostream.
3. Operations
truecm &truecm &
string &O.clear() &clears the contents of O
string &O.str() &returns the current contents of O
All operations and operators (< <) defined for ostreams can be applied to string output streams as well.
7.1.5 Command input streams (cmd_istream)
1. Definition
An instance I of the data type cmd is an istream connected to the output of a shell command cmd, i.e., all input operations or operators applied to I read from the standard output of command cmd.
2. Creation
cmd I(string cmd );
creates an instance I of type cmd_istream connected to the output of command cmd.
3. Operations
All operations and operators (> >) defined for istreams can be applied to command input streams as well.
7.1.6 Command output streams (cmd_ostream)
1. Definition
An instance O of the data type cmd is an ostream connected to the input of a shell command cmd, i.e., all output operations or operators applied to O write into the standard input of command cmd.
2. Creation
cmd O(string cmd );
creates an instance O of type cmd_ostream connected to the input of command cmd.
3. Operations
All operations and operators (< <) defined for ostreams can be applied to command output streams as well.
7.2 Some useful functions and macros
int &read_int(string s = ``'')
&prints s and reads an integer
void &init_random() &initializes the random number generator.
float &used_time() &returns the currently used cpu time in seconds.
newline truecm &cout < < ``"
forever &for(;;)
loop(a,b,c) &for (
a = b;a < = c;a + +)
Max(a,b) &(
(a > b) ? a : b)
Min(a,b) &(
(a > b) ? b : a)
7.3 Memory Management
LEDA offers an efficient memory management system that is used internally for all node, edge and item types. This system can easily be customized for user defined classes by the ``LEDA_MEMORY" macro. You simply have to add the macro call ``LEDA_MEMORY(T)" to the declaration of a class T. This creates new and delete operators for type T allocating and deallocating memory using LEDA's internal memory manager. We continue the example from section 1.5:
struct pair {
&
double x;
&
double y;
&
pair() { x = y = 0; }
&pair(const
pair& p) { x = p.x; y = p.y; }
&friend ostream& &operator(ostream&,const pair&) { ...}
&friend istream& &operator(istream&,pair&) { ...}
&friend int &compare(const pair& p, const pair& q) { ...}
&LEDA_MEMORY(pair)
};
7.4 Error Handling
LEDA tests the preconditions of many (not all!) operations. Preconditions are never tested, if the test takes more than constant time. If the test of a precondition fails an error handling routine is called. It takes an integer error number i and a char* error message string s as arguments. It writes s to the diagnostic output (cerr) and terminates the program abnormally if i≠ 0. Users can provide their own error handling function handler by calling
set_error_handler(handler). After this function call handler is used instead of the default error handler. handler must be a function of type void handler(int, char*). The parameters are replaced by the error number and the error message respectively.
10cm